package com.bailiban.friend.controller;

import com.bailiban.common.entity.Result;
import com.bailiban.common.entity.StatusCode;
import com.bailiban.friend.service.FriendService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhulang
 * @Classname FriendController
 * @Description FriendController
 */
@RestController
@RequestMapping("friend")
public class FriendController {
    @Autowired
    private FriendService friendService;
    @Autowired
    private HttpServletRequest request;

    /**
     * @param friendid
     * @param type     1:喜欢 0：不喜欢
     * @return
     */
    @PostMapping("like/{friendid}/{type}")
    private Result addFriend(@PathVariable String friendid, @PathVariable String type) {
        Claims claims = (Claims) request.getAttribute("claims_roles");
        if (claims != null && "user".equals(claims.get("roles"))) {
            //喜欢
            if ("1".equals(type)) {
                int i = friendService.addFriend(claims.getId(), friendid);
                if (i == 0) {
                    return new Result(false, StatusCode.REPERROR, "你已添加，请勿重复操作");
                }
            } else {
                //不喜欢
                friendService.addNoFriend(claims.getId(), friendid);

            }
            return new Result(true, StatusCode.OK, "添加操作");
        }
        return new Result(false, StatusCode.ACCESSERROR, "没有权限");

    }
}
